package com.book.controller;

import com.book.po.classroom;
import com.book.po.odMap;
import com.book.po.order;
import com.book.po.student;
import com.book.service.orderService;
import com.book.service.classroomService;
import com.book.service.studentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/order")
public class orderController {
    @Autowired
    private orderService orderService;

    @Autowired
    private classroomService classroomService;

    @Autowired
    private studentService studentService;

    @RequestMapping("/order_select")
    public String book_select(RedirectAttributes  model, order order, HttpSession session){
        student student = (student) session.getAttribute("student");

        List<order> orders = orderService.order_select(student.getId());
        if (orders.size()>=1){
            model.addFlashAttribute("orders",orders);
            model.addFlashAttribute("msg","");
            return "redirect:/user/ordermanage";
        }else {
            model.addFlashAttribute("msg","没有预约记录！");
            return "redirect:/user/ordermanage";
        }
    }
    @RequestMapping("/get_order")
    public String get_order(RedirectAttributes  model, order order, String id){

        List<student> student = new ArrayList<student>();
        student.add(studentService.get_student(id));
        List<order> orders = new ArrayList<order>();
        for(student s:student){
            orders.addAll(orderService.order_select(s.getId()));
        }
        if (orders.size()>=1){
            model.addFlashAttribute("orders",orders);
            classroom classroom = new classroom();
            classroom.setId(orders.get(0).getRid());
            List<classroom> classrooms = classroomService.room_select(classroom);
            model.addFlashAttribute("classrooms",classrooms);
            model.addFlashAttribute("users",student);
            model.addFlashAttribute("msg","");
            return "redirect:/manage/bookTypeManage";
        }else {
            model.addFlashAttribute("msg","没有预约记录！");
            return "redirect:/manage/bookTypeManage";
        }
    }


    @RequestMapping("/get_order2")
    public String get_order2(RedirectAttributes  model, classroom classroom){

        List<odMap> maps = classroomService.get_room(classroom);
        if (maps.size()!=0){
            model.addFlashAttribute("maps",maps);
            model.addFlashAttribute("msg","");
            return "redirect:/manage/bookTypeManage";
        }else {
            model.addFlashAttribute("msg","没有预约记录！");
            return "redirect:/manage/bookTypeManage";
        }
    }

    @RequestMapping("/order_insert")
    public String book_insert(RedirectAttributes  model,String sid,String rid,String time,String index){
        order order = new order();
        order.setIndex(index);
        order.setRid(rid);
        order.setSid(sid);
        order.setTime(time);
        classroom classroom = new classroom();
        classroom.setTime(time);
        classroom.setId(rid);
        String msg = orderService.order_insert(order,classroom);

        model.addFlashAttribute("msg",msg);
        return "redirect:/user/siteorder";
    }
////
////    @RequestMapping("/book_update")
////    public String book_update(RedirectAttributes  model, Book book){
////        String msg = bookService.book_update(book);
////        model.addFlashAttribute("msg",msg);
////        return "redirect:/manage/bookManage";
////    }
////
    @RequestMapping("/order_delete")
    public String book_delete(RedirectAttributes  model, String sid){
        String msg = orderService.order_delete(sid);
        model.addFlashAttribute("msg",msg);
        return "redirect:/user/ordermanage";
    }
}
